﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Profari_Heldur_Utan_Um_Prof
{
    
    public partial class CheckBoxes : Form
    {
        #region SQL connection
        private static SqlConnection con = new SqlConnection("Data Source=orninn.com;Initial Catalog=Notendaprofanir;Persist Security Info=True;User ID=SonjaNew;Password=Léttjógúrt");
        private static DataTable dt = new DataTable();
        private static SqlDataAdapter da = new SqlDataAdapter();
        SqlDataReader r = null;
        #endregion

        #region Variables
        int globalGroupsID;
        int globalevaluationID;
        int buttonEmailPressed = 0;        
        #endregion

        #region static variables
        static public int chkIntroSessionValue;
        static public int chkInfoUserBeforeValue;
        static public int chkNameValue;
        static public int chkSsnValue;
        static public int chkMobileValue;
        static public int chkAddressValue;
        static public int chkGenderValue;
        static public int chkBirthYearValue;
        static public int chkOtherValue;
        static public int chkInfoUserAfterValue;
        static public int chkRadQuestions;
        static public int radEssayValue = 0;
        static public int radSurveyValue = 0;
        static public string txtEmailValue;
        static public int chkEmailValue;
        static public int chkUserFeedback;
        #endregion

        public CheckBoxes()
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();    
        }

        private void CheckBoxes_Load(object sender, EventArgs e)
        {
            isChecboxChecked();
            lblEmailInfo.Hide();
            
            txtEmail.Enabled = false;
            
            if (txtEmail.Enabled == true)
            {
                txtEmail.Text = txtEmailValue;
            }

            if (chkIntroSession.CheckState == CheckState.Checked || chkInfoUserAfter.CheckState == CheckState.Checked)
            {
                btnNext.Show();
            }
            else
            {
                btnNext.Hide();
            }
         
        }

        #region Functions
        //Gives the static checkboxes variables value from the checkboxes in this form
        public void getCheckBoxes()
        {
            chkIntroSessionValue = chkIntroSession.CheckState.GetHashCode();
            chkInfoUserBeforeValue = chkInfoUserBefore.CheckState.GetHashCode();
            chkNameValue = chkName.CheckState.GetHashCode();
            chkSsnValue = chkSsn.CheckState.GetHashCode();
            chkMobileValue = chkMobile.CheckState.GetHashCode();
            chkAddressValue = chkAddress.CheckState.GetHashCode();
            chkGenderValue = chkGender.CheckState.GetHashCode();
            chkBirthYearValue = chkBirthYear.CheckState.GetHashCode();
            chkOtherValue = chkOther.CheckState.GetHashCode();
            chkInfoUserAfterValue = chkInfoUserAfter.CheckState.GetHashCode();
            if (radEssay.Checked == true)
            {
                radEssayValue = 1;
                radSurveyValue = 0;
            }
            if (radSurvey.Checked == true)
            {
                radSurveyValue = 1;
                radEssayValue = 0;
            }
            txtEmailValue = txtEmail.Text;
            chkEmailValue = chkEmail.CheckState.GetHashCode();
        }

        //Gives the checkboxes in this form value from the static checkboxes variables
        public void isChecboxChecked()
        {
            if (chkIntroSessionValue.Equals(1))
            {
                chkIntroSession.CheckState = CheckState.Checked;
            }
            if (chkInfoUserBeforeValue.Equals(1))
            {
                chkInfoUserBefore.CheckState = CheckState.Checked;

                if (chkNameValue.Equals(1))
                {
                    chkName.CheckState = CheckState.Checked;
                }
                if (chkSsnValue.Equals(1))
                {
                    chkSsn.CheckState = CheckState.Checked;
                }
                if (chkMobileValue.Equals(1))
                {
                    chkMobile.CheckState = CheckState.Checked;
                }
                if (chkAddressValue.Equals(1))
                {
                    chkAddress.CheckState = CheckState.Checked;
                }
                if (chkGenderValue.Equals(1))
                {
                    chkGender.CheckState = CheckState.Checked;
                }
                if (chkBirthYearValue.Equals(1))
                {
                    chkBirthYear.CheckState = CheckState.Checked;
                }
                if (chkOtherValue.Equals(1))
                {
                    chkOther.CheckState = CheckState.Checked;
                }
            }
            else if (chkInfoUserBefore.Equals(0))
            {
                chkNameValue.Equals(0);
                chkSsnValue.Equals(0);
                chkMobileValue.Equals(0);
                chkAddressValue.Equals(0);
                chkGenderValue.Equals(0);
                chkBirthYearValue.Equals(0);
                chkOtherValue.Equals(0);
            }
            if (chkInfoUserAfterValue.Equals(1))
            {
                chkInfoUserAfter.CheckState = CheckState.Checked;

                if (radEssayValue == 1)
                {
                    radEssay.Checked = true;
                }
                else if (radSurveyValue == 1)
                {
                    radSurvey.Checked = true;
                }
            }
            if (chkEmailValue.Equals(1))
            {
                chkEmail.CheckState = CheckState.Checked;
                txtEmail.Text = txtEmailValue;
            }
        }

        #endregion

        #region Buttons
        private void btnNext_Click(object sender, EventArgs e)
        {
            //If the user checks that he wants a introduction he will be moved to introduction window when next button is pressed
            if (chkIntroSession.Checked)
            {
                 //getCheckBoxes();
                 System.Threading.Thread introductionWindow = new System.Threading.Thread(new System.Threading.ThreadStart(OpenIntroductionText));
                 introductionWindow.SetApartmentState(System.Threading.ApartmentState.STA);
                 introductionWindow.Start();
                 this.Close();
            }
            else if(chkInfoUserAfter.Checked)
            {
                if(radEssay.Checked == true)
                {
                    System.Threading.Thread testerInterface = new System.Threading.Thread(new System.Threading.ThreadStart(OpenCreateEssay));
                    testerInterface.SetApartmentState(System.Threading.ApartmentState.STA);
                    testerInterface.Start();
                    this.Close();
                }
                else if(radSurvey.Checked == true)
                {
                    System.Threading.Thread testerInterface = new System.Threading.Thread(new System.Threading.ThreadStart(OpenCreateSurvey));
                    testerInterface.SetApartmentState(System.Threading.ApartmentState.STA);
                    testerInterface.Start();
                    this.Close();
                }
            }
            getCheckBoxes();
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //If cancel button is pressed user get's a question if he wants to quit making a project or not
            DialogResult cancelProject = MessageBox.Show("Are you sure you want to stop making new project?", "Quit", MessageBoxButtons.YesNo);
            if (cancelProject == DialogResult.Yes)
            {
                System.Threading.Thread testerInterface = new System.Threading.Thread(new System.Threading.ThreadStart(OpenTesterInterface));
                testerInterface.SetApartmentState(System.Threading.ApartmentState.STA);
                testerInterface.Start();
                this.Close();
            }
        }
 
        private void btnBack_Click(object sender, EventArgs e)
        {
            //User is moved to the form where he can put in url of the website
            getCheckBoxes();
            System.Threading.Thread webPage = new System.Threading.Thread(new System.Threading.ThreadStart(OpenCreateTask));
            webPage.SetApartmentState(System.Threading.ApartmentState.STA);
            webPage.Start();
            this.Close();
        }

        private void btnEmailAddressInfo_Click(object sender, EventArgs e)
        {
            if (buttonEmailPressed % 2 == 1)
            {
                lblEmailInfo.Hide();
                buttonEmailPressed++;
            }
            else if (buttonEmailPressed % 2 == 0)
            {
                lblEmailInfo.Show();
                buttonEmailPressed++;
            }
        }

        private void btnFinish_Click(object sender, EventArgs e)
        {
            if (CreateTask.globalGroupID == 0)
            {
                insertGroups();
                globalGroupsID = getGroupsID();
            }
            else
                globalGroupsID = CreateTask.globalGroupID;
            insertEvaluation();
            
            globalevaluationID = getEvaluationID();
            insertGroupEvaluations(globalGroupsID, globalevaluationID);
            insertTasks();
            getTaskID();
            insertEvaluationTask();
            getCheckBoxes();
            insertCheckBoxInfo();
            System.Threading.Thread testerInterface = new System.Threading.Thread(new System.Threading.ThreadStart(OpenTesterInterface));
            testerInterface.SetApartmentState(System.Threading.ApartmentState.STA);
            testerInterface.Start();
            CreateTask.taskList.Clear();
            CreateTask.TaskID.Clear();
            this.Close();
        }


        
        #endregion

        #region SQL commands
        private void insertGroups()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO Groups(name) values('" + CreateTask.txtCreateGroupNameValue + "')", con);
                cmd.ExecuteNonQuery();

            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());

            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
        }

        private void insertEvaluation()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO Evaluation(name,url,email) values(@txtname, @txturl, @txtemail)", con);
                cmd.Parameters.AddWithValue("@txtname", (CreateTask.txtEvaluationNameValue == "" ? DBNull.Value : (object)CreateTask.txtEvaluationNameValue));
                cmd.Parameters.AddWithValue("@txturl", (CreateTask.txtWebPageValue == "" ? DBNull.Value : (object)CreateTask.txtWebPageValue));
                cmd.Parameters.AddWithValue("@txtemail", (txtEmail.Text == "" ? DBNull.Value : (object)txtEmail.Text));
                cmd.ExecuteNonQuery();

            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());

            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
        }

        private int getGroupsID()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select top 1 id from Groups order by id desc", con);
                r = cmd.ExecuteReader();
                while (r.Read())
                {
                    return r.GetInt32(0);
                }

            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());

            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
            return 0;
        }

        private int getEvaluationID()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select top 1 id from Evaluation order by id desc", con);
                r = cmd.ExecuteReader();
                while (r.Read())
                {
                    return r.GetInt32(0);
                }

            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());

            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
            return 0;
        }

        private void insertGroupEvaluations(int GroupsID, int EvaluationID)
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO GroupEvaluations(GroupsID,evaluationID) values('" + GroupsID + "','" + EvaluationID + "')", con);
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());

            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
        }

        private void insertTasks()
        {

            foreach (CreateTask.Tasks TaskObj in CreateTask.taskList)
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("INSERT INTO task(name, taskDescription, scenariosDescription) VALUES(@txtname, @txtTaskDescript, @txtScenario)", con);
                    cmd.Parameters.AddWithValue("@txtname", (TaskObj.task == "" ? DBNull.Value : (object)TaskObj.task));
                    cmd.Parameters.AddWithValue("@txtTaskDescript", (TaskObj.taskDescript == "" ? DBNull.Value : (object)TaskObj.taskDescript));
                    cmd.Parameters.AddWithValue("@txtScenario", (TaskObj.scenarioTask == "" ? DBNull.Value : (object)TaskObj.scenarioTask));
                    cmd.ExecuteNonQuery();

                }
                catch (SqlException ex)
                {
                    Console.WriteLine("Error: {0}", ex.ToString());

                }
                finally
                {
                    if (con != null)
                    {
                        con.Close();
                    }
                }
            }
        }

        private void getTaskID()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select top " + CreateTask.taskList.Count + " id from task order by id desc", con);
                r = cmd.ExecuteReader();
                while (r.Read())
                {
                    if (!r.IsDBNull(0))
                    {
                        CreateTask.TaskID.Add(r.GetInt32(0));
                    }

                }

            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());

            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
        }

        private void insertEvaluationTask()
        {
            foreach (int id in CreateTask.TaskID)
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("INSERT INTO EvaluationTask(evaluationID, taskID, introBeforeEvaluationID) values(@evaluationID, @taskID, @introBFEvalID)", con);
                    cmd.Parameters.AddWithValue("@evaluationID", globalevaluationID==0 ? DBNull.Value : (object)globalevaluationID);
                    cmd.Parameters.AddWithValue("@taskID", id == 0 ? DBNull.Value : (object)id);
                    cmd.Parameters.AddWithValue("@introBFEvalID",DBNull.Value);
                    cmd.ExecuteNonQuery();

                }
                catch (SqlException ex)
                {
                    Console.WriteLine("Error: {0}", ex.ToString());

                }
                finally
                {
                    if (con != null)
                    {
                        con.Close();
                    }
                }
            }
        }

        private void insertCheckBoxInfo()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO checkBoxInfo(groupsID, evaluationID, introBeforeEvaluationID, name, ssn,mobile,addres,gender,birthYear,other,userFeedback)" +
                                                "values('" + globalGroupsID + "','" + globalevaluationID + "','" + chkIntroSessionValue + "','" + chkNameValue +
                                                "','" + chkSsnValue + "','" + chkMobileValue + "','" + chkAddressValue + "','" + chkGenderValue +
                                                "','" + chkBirthYearValue + "','" + chkOtherValue + "','" + chkInfoUserAfterValue + "')", con);
                cmd.ExecuteNonQuery();

            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());

            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
        }

        #endregion
        
        #region Code for checkboxes
        private void chkIntroSession_CheckedChanged(object sender, EventArgs e)
        {
            if (chkIntroSession.CheckState == CheckState.Checked || chkInfoUserAfter.CheckState == CheckState.Checked)
            {
                this.pictureBox1.Show();
                pictureBox1.Load("PreviewIntroSession.PNG");
                btnNext.Show();
                btnFinish.Hide();
            }
            else
            {
                this.pictureBox1.Hide();
                btnNext.Hide();
                btnFinish.Show();
            }
        }

        private void chkInfoUserBefore_CheckedChanged(object sender, EventArgs e)
        {
            if (chkInfoUserBefore.CheckState == CheckState.Checked)
            { 
                chkName.Enabled = true;
                chkSsn.Enabled = true;
                chkMobile.Enabled = true;
                chkAddress.Enabled = true;
                chkGender.Enabled = true;
                chkBirthYear.Enabled = true;
                chkOther.Enabled = true;
                this.pictureBox1.Show();
                pictureBox1.Load("PreviewInformationBefore.PNG");
            }                           
            else
            {
                chkName.Enabled = false;
                chkSsn.Enabled = false;
                chkMobile.Enabled = false;
                chkAddress.Enabled = false;
                chkGender.Enabled = false;
                chkBirthYear.Enabled = false;
                chkOther.Enabled = false;
                pictureBox1.Hide();
            }
        }

        private void chkInfoUserAfter_CheckedChanged(object sender, EventArgs e)
        {
            if (chkInfoUserAfter.CheckState == CheckState.Checked)
            {
                radEssay.Enabled = true;
                radSurvey.Enabled = true;
                
               // btnNext.Show();
                //btnFinish.Hide();
            }
            else
            {
                if (chkIntroSession.CheckState == CheckState.Checked)
                {
                    btnFinish.Hide();
                    btnNext.Show();
                }
                else
                {
                    btnFinish.Show();
                    btnNext.Hide();
                }

                radEssay.Enabled = false;
                radSurvey.Enabled = false;
            }
        }

        private void chkEmail_CheckedChanged(object sender, EventArgs e)
        {
            if (chkEmail.CheckState == CheckState.Checked)
            {
                txtEmail.Enabled = true;
                this.pictureBox1.Show();
                pictureBox1.Load("PreviewEmailAddress.PNG");
            }
            else
            {
                txtEmail.Enabled = false;
                this.pictureBox1.Hide();
            }
        }

        private void radEssay_CheckedChanged(object sender, EventArgs e)
        {
            if (chkInfoUserAfter.CheckState == CheckState.Checked || chkIntroSession.CheckState == CheckState.Checked)
            {
                if (radEssay.Checked)
                {
                    this.pictureBox1.Show();
                    pictureBox1.Load("PreviewCreateEssay.PNG");
                    btnNext.Show();
                    btnFinish.Hide();
                }
                else
                {
                    this.pictureBox1.Hide();
                    btnNext.Hide();
                    btnFinish.Show();
                }
            }

        }

        private void radSurvey_CheckedChanged(object sender, EventArgs e)
        {
            if (chkInfoUserAfter.CheckState == CheckState.Checked || chkIntroSession.CheckState == CheckState.Checked)
            {
                if (radSurvey.Checked)
                {
                    this.pictureBox1.Show();
                    pictureBox1.Load("PreviewCreateSurvey.PNG");
                    btnNext.Show();
                    btnFinish.Hide();
                }
                else
                {
                    this.pictureBox1.Hide();
                    btnNext.Hide();
                    btnFinish.Show();
                }
            }

        }
        #endregion

        //The Open forms does it able to go from one form to another form and the first form will be closed when we want to 
        //open the second form.
        #region OpenForms
        public void OpenTesterInterface()
        {
            Application.Run(new TesterInterface());
        }

        public void OpenCreateTask()
        {
            Application.Run(new CreateTask());
        }

        public void OpenIntroductionText()
        {
            Application.Run(new IntroductionText());
        }

        public static void OpenCreateEssay()
        {
            Application.Run(new CreateEssay());
        }

        public static void OpenCreateSurvey()
        {
            Application.Run(new CreateSurvey());
        }

        #endregion

    }
}
